import { createRouter, createWebHistory } from 'vue-router'
import PKindexView from '../views/pk/PKindexView'
import RanklistindexView from '../views/ranklist/RanklistindexView'
import RecordindexView from '../views/record/RecordindexView'
import RecordContentView from '@/views/record/RecordContentView'
import UserindexView from '@/views/user/bot/UserindexView'
import NotFound from '../views/error/NotFound'
import UserAccountLogin from '@/views/user/account/UserAccountLogin'
import UserAccountRegister from '@/views/user/account/UserAccountRegister'
import store from '../store/index'
const routes = [
    {
      path:"/",
      redirect:"/pk/",
      name:"home",
      meta:{
        requestAuth:true
      }
    },
    {
      path:"/pk/",
      component:PKindexView,
      name:"pk_index",
      meta:{
        requestAuth:true
      }
    },
    {
      path:"/record/",
      component:RecordindexView,
      name:"record_index",
      meta:{
        requestAuth:true
      }
    },
    {
      path:"/record/:recordId/",
      component:RecordContentView,
      name:"record_content",
      meta:{
        requestAuth:true
      }
    },
    {
      path:"/ranklist/",
      component:RanklistindexView,
      name:"ranklist_index",
      meta:{
        requestAuth:true
      }
    },
    {
      path:"/user/",
      component:UserindexView,
      name:"user_index",
      meta:{
        requestAuth:true
      }
    },
    {
      path:"/user/account/login",
      component:UserAccountLogin,
      name:"login",
      meta:{
        requestAuth:false
      }
    },
    {
      path:"/user/account/register",
      component:UserAccountRegister,
      name:"register",
      meta:{
        requestAuth:false
      }
    },
    {
      path:"/error/",
      component:NotFound,
      name:"404",
      meta:{
        requestAuth:false
      }
    },
    {
      path:"/:catchAll(.*)",
      redirect:"/error/"
    },
]

const router = createRouter({
  history: createWebHistory(),
  routes
})
router.beforeEach((to,from,next) => {
  if(to.meta.requestAuth && !store.state.user.isLogin){
    next({name:'login'})
  }else{
    next() 
  }
})
export default router
